<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="日期" prop="operTimeRange">
        <el-date-picker size="small" :clearable="false"
          v-model="queryParams.operTimeRange"
          style="width: 250px;"
          type="daterange"
          value-format="yyyy-MM-dd"
          placeholder="选择查询日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="客户" prop="organId">
        <el-select
          v-model="queryParams.customerId"
          placeholder="请选择客户"
          filterable
          clearable
          size="small"
          style="width: 250px;"
          @keyup.enter.native="handleQuery"
        >
          <el-option
            v-for="menber in customers"
            :key="menber.id"
            :label="menber.supplier"
            :value="menber.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">

    </el-row>

    <el-table v-loading="loading" max-height="620px" :data="detailList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="客户" prop="customerId" :formatter="organFormat" />
      <el-table-column label="销售额" align="center" prop="saleTotal" />
      <el-table-column label="采购额" align="center" prop="purchaseTotal" />
      <el-table-column label="利润" align="center" prop="profit" />
      <el-table-column label="利润比" align="center" prop="profitRatio" >
        <template slot-scope="scope">
          <span>{{scope.row.profitRatio}}%</span>
        </template>>
      </el-table-column>
      <el-table-column label="明细" align="center" class-name="small-padding fixed-width">
              <template slot-scope="scope">
                <!-- <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-edit"
                    @click="handleCustomerDepot(scope.row.organId)"
                >单据列表</el-button> -->
                <router-link :to="{path :'/report/outDetail',query:{'customerId':scope.row.customerId,'operTimeRange':queryParams.operTimeRange}}">
                  单据列表
                </router-link>
          </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

  </div>
</template>

<script>
import { bossReport } from "@/api/system/detail";

export default {
  name: "Detail",
  data() {
    return {
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 统计利润明细表格数据
      detailList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        customerId: null,
        params:{
          createTimeBegin:null,
          createTimeEnd:null,
        },
        operTimeRange:[],
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      //客户下拉框数据
      customers:[],
    };
  },
  beforeCreate() {
    this.getSuppliers({'type':'客户'}).then(response => {
      this.customers = response.data;
    });
  },
  watch:{
    'queryParams.operTimeRange':function(n,o){
      if(n.length>0){
        this.queryParams.params.createTimeBegin = n[0] +" 00:00:00"
        this.queryParams.params.createTimeEnd = n[1] +" 23:59:59"
      }
    }
  },
  created() {
    this.queryParams.operTimeRange = [this.parseTime(new Date(),'{y}-{m}-{d}'),this.parseTime(new Date(),'{y}-{m}-{d}')];
    // this.getList();
  },
  methods: {
    handleCustomerDepot(customerId){
        // this.$router.push("/report/outDetail/index" );
        this.$router.push({path :'/report/outDetail/index',query:{'customerId':customerId,'operTimeRange':this.queryParams.operTimeRange}});
    },
    //客户id转为名称
    organFormat(row, column) {
      var actions = [];
      this.customers.filter(item => {
      	if (item.id == row.customerId) {
      		actions.push(item.supplier);
      	}
      })
      return actions.join('');
    },
    /** 查询统计利润明细列表 */
    getList() {
      this.loading = true;
      bossReport(this.queryParams).then(response => {
        this.detailList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        inHeadId: null,
        inItemId: null,
        outHeadId: null,
        outItemId: null,
        outOperTime: null,
        matrialId: null,
        matrialExtendId: null,
        customerId: null,
        operNumber: null,
        basicNumber: null,
        profit: null,
        inPrice: null,
        outPrice: null,
        tenantId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/detail/export', {
        ...this.queryParams
      }, `detail_${new Date().getTime()}.xlsx`)
    },
    handleCompute(){
      this.$modal.confirm('是否确认进行利润计算？').then(function() {
        return computeDetail();
      }).then((res) => {
        this.getList();
        this.$modal.msgSuccess(res.msg);
      }).catch(() => {});
    }
  }
};
</script>
